# Title: 
# Autho: Laguna Hernandez Biridiana 14210508
# Description:
# Input:-
# Output:


@ Numero.s
@ Obtener la entrada del usuario listo para hacer las matematicas
@ registers
@ r1 - pointer to the buffer
@ r2 - used to load digits
@ r3 - counts digits
@ r4 - multiplier
@ r6 - counter
@ r0 - return value

.section        .bss
.comm buffer, 48        @

.section        .data
msg:
        .ascii  "Enter a number: "
msgLen = .- msg

.section        .text
.globl  _start
_start:



mov r0, $1                      @
ldr r1, =msg
ldr r2, =msgLen
mov r7, $4
svc $0


                                @
mov r7, $3                      @
mov r0, $1
ldr r1, =buffer
mov r2, $0x30
svc $0


                                @
ldrb r2, [r1]                   @
mov r3, $0                      @


pushDigits:
stmfd   sp!, {r2}               @
add     r3, r3, $1              @
ldrb    r2, [r1, #1]!           @
cmp     r2, $0xA                @
beq     convDigits              @
bne     pushDigits              @


convDigits:
mov  r4, $1                     @
mov  r0, $0                     @
mov  r6, $0                     @


jumpBack:
ldmfd  sp!, {r2}                @
cmp    r2, $0x39                @
blt    error
cmp    r2, $0x39                @
bgt    error
sub    r2, r2, $0x30            @
mul    r2, r4, r2               @
add    r0, r0, r2               @
add    r6, r6, $1               @
cmp    r6, r3                   @
beq    exit
mov    r5, r4, lsl $3                   @
add    r4, r5, r4, lsl $1               @
bal    jumpBack

@ Least significant byte available via "echo $?"

error:
mov r0, $-1
bal exit

exit:
mov r7, $1      @
svc $0          @
.end
